//
//  LoginController.m
//  SmallElephant
//
//  Created by Faith on 2024/11/11.
//

#import "LoginController.h"
#import "LoginWithAccountController.h"
#import "SignupController.h"

@interface LoginController ()

@end

@implementation LoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view applyStyle:@"background: #FFFFFF;"];
    [self setTitleText:@"手机号码登录"];
    [self.btnLeft setImage:[UIImage systemImageNamed:@"xmark"] forState:UIControlStateNormal];
    
    UILabel *welcomeView = [[UILabel alloc] init];
    welcomeView.text = @"欢迎登录小象超市";
    [self.view addSubview:welcomeView style:@"display: flex; flex-direction: row; font-size: 24px; color: #111111; margin-left: 20px; margin-top: 40px; margin-right: 20px;"];
    
    UIView *countrySelectView = [[UIView alloc] init];
    [self.view addSubview:countrySelectView style:@"display: flex; flex-direction: row; justify-content: space-between; margin-left: 20px; align-items: center; margin-top: 30px; margin-right: 20px; background: #F6F6F6; border-radius: 22px; height: 44px;"];
    UILabel *countrySelectTitle = [[UILabel alloc] init];
    countrySelectTitle.numberOfLines = 0;
    countrySelectTitle.text = @"国家/地区";
    [countrySelectView addSubview:countrySelectTitle style:@"display: flex; color: #666666; font-size: 14px; margin-left: 15px;"];
    
    UILabel *countrySelectValue = [[UILabel alloc] init];
    countrySelectValue.text = @"中国(+86)";
    [countrySelectView addSubview:countrySelectValue style:@"display: flex; color: #666666; font-size: 14px; margin-right: 15px;"];
    
    // 手机号码输入框
    UIView *phoneView = [[UIView alloc] init];
    [self.view addSubview:phoneView style:@"display: flex; flex-direction: row; margin-left: 20px; align-items: center; margin-top: 20px; margin-right: 20px; background: #F6F6F6; border-radius: 22px; height: 44px;"];
    UITextField *phoneInputView = [[UITextField alloc] init];
    phoneInputView.placeholder = @"请输入手机号码";
    phoneInputView.keyboardType = UIKeyboardTypeNumberPad;
    [phoneView addSubview:phoneInputView style:@"display: flex; flex:1; color: #666666; font-size: 14px; margin-left: 15px; margin-right: 15px;"];
    
    // 手机号码输入框
    PHCaptchaView *captchaView = [[PHCaptchaView alloc] init];
    [self.view addSubview:captchaView style:@"display: flex; flex-direction: row; margin-left: 20px; align-items: center; margin-top: 20px; margin-right: 20px; height: 50px;"];
    
    
    // 隐私协议
    UIView *privacyView = [[UIView alloc] init];
    [self.view addSubview:privacyView style:@"display: flex; flex-direction: row; margin-left: 20px; align-items: center; margin-top: 15px; margin-right: 20px;"];
    
    UIButton *btnAgree = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnAgree setImage:[UIImage imageWithName:@"icon-unchecked@3x"] forState:UIControlStateNormal];
    [btnAgree setImage:[UIImage imageWithName:@"icon-checked@3x"] forState:UIControlStateSelected];
    [privacyView addSubview:btnAgree style:@"display: flex; width: 30px; height: 30px;"];
    [btnAgree addTarget:self action:@selector(btnAgreeClickEventHandler:) forControlEvents:UIControlEventTouchUpInside];
   
    UILabel *privacyTip = [[UILabel alloc] init];
    privacyTip.textColor = [UIColor colorWithHexString:@"#666666"];
    NSMutableAttributedString *privacyText = [[NSMutableAttributedString alloc] initWithString:@"我已阅读并同意《服务条款》和《隐私协议》"];
    [privacyText addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#EF2020"] range:NSMakeRange(7, 6)];
    [privacyText addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#EF2020"] range:NSMakeRange(14, 6)];
    privacyTip.attributedText = privacyText;
    [privacyView addSubview:privacyTip style:@"display: flex; font-size: 14px;"];
    
    // 登录按钮
    UIButton * btnSendCaptcha = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnSendCaptcha setTitle:@"获取验证码" forState:UIControlStateNormal];
    [btnSendCaptcha setBackgroundColor:[UIColor colorWithHexString:@"#EF2020"]];
    [btnSendCaptcha addTarget:self action:@selector(btnAgreeClickEventHandler:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnSendCaptcha style:@"display: flex; height:44px; border-radius: 5px; margin-left: 20px; font-size: 16px; margin-top: 40px; margin-right: 20px;"];
    
    // 账号密码登录按钮
    UIView *accountLoginView = [[UIView alloc] init];
    [self.view addSubview:accountLoginView style:@"display: flex; flex-direction: row; justify-content: space-between;  margin-left: 20px; align-items: center; margin-top: 20px; margin-right: 20px;"];
    
    UIButton * btnAccountLogin = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnAccountLogin setTitle:@"账号密码登录" forState:UIControlStateNormal];
    [btnAccountLogin addTarget:self action:@selector(btnAccountLoginClickEventHandler) forControlEvents:UIControlEventTouchUpInside];
    [accountLoginView addSubview:btnAccountLogin style:@"display: flex; border-radius: 5px; color: #666666; font-size: 14px;"];
    
    UIButton *btnSignup = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnSignup setTitle:@"还没有账号? 立即注册" forState:UIControlStateNormal];
    [btnSignup addTarget:self action:@selector(btnSignupClickEventHandler) forControlEvents:UIControlEventTouchUpInside];
    [accountLoginView addSubview:btnSignup style:@"display: flex; border-radius: 5px; color: #EF2020; font-size: 14px;"];
}

/// 复选框点击事件
- (void)btnAgreeClickEventHandler:(UIButton*)button {
    button.selected = !button.selected;
}

- (void)btnAccountLoginClickEventHandler {
    LoginWithAccountController *controller = [[LoginWithAccountController alloc] init];
    [self pushViewController:controller];
}

- (void)btnSignupClickEventHandler {
    SignupController *controller = [[SignupController alloc] init];
    [self pushViewController:controller];
}

@end
